package com.chat.util;


import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisUtil {
    private static final GenericObjectPoolConfig poolConfig;

    private static final JedisPool jedisPool;

    static {
        poolConfig=new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL*2);
        poolConfig.setMaxWaitMillis(5000);

        jedisPool= new JedisPool(poolConfig,"127.0.0.1",6379);
    }

    private RedisUtil(){}

    public static Jedis getJedis(){
        Jedis jedis=null;
        try {
            jedis=jedisPool.getResource();
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }

        return jedis;
    }
}
